package fr.opensagres.xdocreport.samples.docxandvelocity.web.servlet;

import java.io.IOException;
import java.io.InputStream;

import javax.servlet.http.HttpServletRequest;

import fr.opensagres.xdocreport.core.XDocReportException;
import fr.opensagres.xdocreport.core.XDocReportNotFoundException;
import fr.opensagres.xdocreport.document.web.AbstractProcessXDocReportServlet;
import fr.opensagres.xdocreport.samples.odtandvelocity.model.Project;
import fr.opensagres.xdocreport.template.IContext;

public class MyReportServlet extends
                AbstractProcessXDocReportServlet {

  public static final String REPORT_ID = "DocxProjectWithVelocity.docx";

  @Override
  protected InputStream getSourceStream(String reportId,
                HttpServletRequest request) throws IOException, XDocReportException {
    if (REPORT_ID.equals(reportId)) {
      return MyReportServlet.class.getResourceAsStream("DocxProjectWithVelocity.docx");
    }
    // else manage another report
    throw new XDocReportNotFoundException(reportId);
  }

  @Override
  protected void populateContext(IContext context, String reportId,
                        HttpServletRequest request) throws IOException, XDocReportException {
    if (REPORT_ID.equals(reportId)) {
      String name = request.getParameter("name");
      Project project = new Project(name);
      context.put("project", project);
    }
    // else manage another report
  }
}